package com.ss.android.newmedia.a;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import android.os.IBinder;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.util.DeviceUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public final class InputMethodManagerUtil {
    private static boolean a = true;

    public static void fixMemoryLeak(Activity activity) {
        int v1 = 0;
        if ((InputMethodManagerUtil.a) && activity != null && (activity.isFinishing())) {
            try {
                Object v2 = activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                Field v3 = InputMethodManager.class.getDeclaredField("sInstance");
                v3.setAccessible(true);
                if (v2 != v3.get(null)) {
                    return;
                }

                if (Build.VERSION.SDK_INT >= 23) {
                    v1 = 1;
                }

                if (v1 == 0) {
                    v3 = InputMethodManager.class.getDeclaredField("mCurRootView");
                    v3.setAccessible(true);
                    Object v0_1 = v3.get(v2);
                    if (!(v0_1 instanceof View)) {
                        return;
                    } else if (InputMethodManagerUtil.findActivity(((View) v0_1)) == activity) {
                        v3.set(v2, null);
                    } else {
                        return;
                    }
                }

                IBinder v0_2 = activity.getWindow().getDecorView().getWindowToken();
                if (v0_2 != null) {
                    Method v3_1 = InputMethodManager.class.getDeclaredMethod("windowDismissed", IBinder.class);
                    v3_1.setAccessible(true);
                    v3_1.invoke(v2, v0_2);
                }

                if (v1 == 0) {
                    Method v0_3 = InputMethodManager.class.getDeclaredMethod("startGettingWindowFocus", View.class);
                    v0_3.setAccessible(true);
                    v0_3.invoke(v2, ((Object) null));
                }

                Field v0_4 = InputMethodManager.class.getDeclaredField("mServedView");
                v0_4.setAccessible(true);
                InputMethodManagerUtil.releaseField(v0_4, v2, activity);
                v0_4 = InputMethodManager.class.getDeclaredField("mNextServedView");
                v0_4.setAccessible(true);
                InputMethodManagerUtil.releaseField(v0_4, v2, activity);
                if (!DeviceUtils.isHuawei()) {
                    Logger.d("InputMethodManagerUtil", "fix memeory leak done " + activity);
                    return;
                }

                try {
                    v0_4 = InputMethodManager.class.getDeclaredField("mLastSrvView");
                    v0_4.setAccessible(true);
                    InputMethodManagerUtil.releaseField(v0_4, v2, activity);
                } catch (NoSuchFieldException v0_5) {
                }
            } catch (Throwable v0) {
                Logger.d("InputMethodManagerUtil", "fix memory leak exception: " + v0);
                return;
            }
            Logger.d("InputMethodManagerUtil", "fix memeory leak done " + activity);
        }
    }

    public static void a(boolean arg0) {
        InputMethodManagerUtil.a = arg0;
    }

    private static Activity findActivity(View arg5) {
        Context v0 = arg5 != null ? arg5.getContext() : null;
        while (true) {
            if (v0 == null) {
                return null;
            } else if (!(v0 instanceof Activity)) {
                if ((v0 instanceof ContextWrapper)) {
                    v0 = ((ContextWrapper) v0).getBaseContext();
                    continue;
                } else {
                    Logger.w("InputMethodManagerUtil", "find non-ContextWrapper in view: " + v0);
                    return null;
                }
            }

            return ((Activity) v0);
        }
    }

    private static void releaseField(Field field, Object arg4, Activity activity) throws Throwable {
        int v0_1;
        Object v0 = field.get(arg4);
        if (!(v0 instanceof View) || activity == null) {
            v0_1 = 0;
        } else if (InputMethodManagerUtil.findActivity(((View) v0)) == activity) {
            v0_1 = 1;
        } else {
            v0_1 = 0;
        }

        if (v0_1 != 0) {
            field.set(arg4, null);
        }
    }
}

